package com.yuantu.judiciary.utils;

import java.security.MessageDigest;
import java.util.Random;

/**
 * 短链接工具类
 *
 * @author syw
 * @since 2023-07-17
 **/
public class UrlUtil {

    /**
     * 生成唯一性短链接
     */
    public static synchronized String getUniqueShortUrl(String url) {
        String shortUrl = getShortUrl(url);
        String shortTime = getShortUrl(String.valueOf(System.currentTimeMillis()));
        return shortUrl + shortTime;
    }

    /**
     * 根据MD5加密方式生成短链接
     */
    public static String getShortUrl(String url) {
        // 可以自定义生成 MD5 加密字符传前的混合 KEY
        String key = "XYCXB";
        // 要使用生成 URL 的字符
        String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h",
                "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
                "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
                "U", "V", "W", "X", "Y", "Z"

        };
        // 对传入网址进行 MD5 加密
        String hex = md5ByHex(key + url);

        String[] resUrl = new String[4];
        for (int i = 0; i < 4; i++) {

            // 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算
            String sTempSubString = hex.substring(i * 8, i * 8 + 8);

            // 这里需要使用 long 型来转换，因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 , 如果不用long ，则会越界
            long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
            StringBuilder outChars = new StringBuilder();
            for (int j = 0; j < 6; j++) {
                // 把得到的值与 0x0000003D 进行位与运算，取得字符数组 chars 索引
                long index = 0x0000003D & lHexLong;
                // 把取得的字符相加
                outChars.append(chars[(int) index]);
                // 每次循环按位右移 5 位
                lHexLong = lHexLong >> 5;
            }
            // 把字符串存入对应索引的输出数组
            resUrl[i] = outChars.toString();
        }
        Random random = new Random();
        int j = random.nextInt(4);
        //随机取一个作为短链
        return resUrl[j];
    }

    /**
     * MD5加密(32位大写)
     */
    public static String md5ByHex(String src) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] b = src.getBytes();
            md.reset();
            md.update(b);
            byte[] hash = md.digest();
            StringBuilder hs = new StringBuilder();
            String stmp;
            for (byte value : hash) {
                stmp = Integer.toHexString(value & 0xFF);
                if (stmp.length() == 1) {
                    hs.append("0").append(stmp);
                } else {
                    hs.append(stmp);
                }
            }
            return hs.toString().toUpperCase();
        } catch (Exception e) {
            return "";
        }
    }

    public static void main(String[] args) {
        for (int q = 0; q < 10 ; q++) {
            System.out.println(UrlUtil.getUniqueShortUrl("cfc4ea1a726724f1e77b944d05b8f710b8108cd9cd5e00fe4637ee032f4c48c90dd8902c0545bdc1da3718084f6e0d71db80d2d198784f0779c8ae4f792140ca35b1331d5ecebc321b78175eac0d2090a117f3e768fe6ec01a3333c283699002f62e71f16b6e6b643961b30d9b0c5c2666159d0a1288eaf79b485f317eed923e60c6b1918f3f13b91c201ad80bdec662e4dc301de4ed7e1ceaeb7ff2a61a9c97b45c16644fe737bd1dd9281133d3b42b8ca4ecb3ea21d6d99304f48a7031708fbd40242c488e3240f451d2f882c773d3920f64e8e910c11b3d5c7f"));
        }
    }
}

